package com.dingdayu.club.admin.api;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.dingdayu.club.admin.LoginActivity;
import com.dingdayu.club.admin.common.util.HttpUtils;
import com.loopj.android.http.HttpGet;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.http.HttpStatus;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.KJLoger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dingdayu on 2015-10-22.
 */
public class LoginApi {
    static Boolean login_ret = false;
    public static String USER_INFO_FILE = "user_info";

    public static Boolean logint(String mEmail,String mPassword){
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", mEmail);
        params.put("pwd", mPassword);
        params.put("md5", "0");
        String t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/login", params, "utf-8");
        Log.e("LoginAPI", t);

        if(t == null){
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
            return false;
        }
        try {
            JSONObject jsonObj = new JSONObject(t);
            int code = jsonObj.getInt("code");
            String msg = jsonObj.getString("msg");
            if (code == 400) {
                //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
                return false;
            }

            if (code == 200) {
                JSONObject data = jsonObj.getJSONObject("data");
                int uid = data.getInt("uid");
                String nickname = data.getString("nickname");
                String username = data.getString("username");
                int corp = data.getInt("corp_id");
                String phone = data.getString("phone");
                String email = data.getString("email");
                saveLogin(username, nickname, corp, phone, email);
                return true;
            }
        }catch (JSONException e) {
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
            Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
            return false;
            //login_ret = false;
            //e.printStackTrace();
        }
        return false;
    }

    public static Boolean login(String mEmail,String mPassword){
        KJHttp kjh = new KJHttp();
        HttpParams params2 = new HttpParams();
        params2.put("username", mEmail); //传递参数
        params2.put("pwd", mPassword);
        params2.put("md5", "0"); //传递参数

        //HttpCallback中有很多方法，可以根据需求选择实现
        kjh.post("http://corp.xyser.com/Home/Api/login", params2, new HttpCallBack() {
            @Override
            public void onSuccess(String t) {
                if(t == null){
                    new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
                    login_ret = false;
                    super.onSuccess(t);
                }
                ViewInject.longToast("请求成功");
                Log.e("LoginAPI", t);
                try {
                    JSONObject jsonObj = new JSONObject(t);
                    int code = jsonObj.getInt("code");
                    String msg = jsonObj.getString("msg");
                    if(code == 400){
                        new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定",null).show();
                        login_ret = false;
                        super.onSuccess(t);
                    }

                    if(code == 200){
                        JSONObject data =jsonObj.getJSONObject("data");
                        int uid = data.getInt("uid");
                        String nickname = data.getString("nickname");
                        String username = data.getString("username");
                        int corp = data.getInt("corp_id");
                        String phone = data.getString("phone");
                        String email = data.getString("email");
                        saveLogin(username, nickname, corp, phone, email);
                        login_ret = true;
                    }
                } catch (JSONException e) {
                    new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
                    Log.e("LoginAPI", "返回数据解析错误");
                    login_ret = false;
                    e.printStackTrace();
                }
                super.onSuccess(t);
            }
        });

        if(login_ret){
            Log.e("LoginAPI", "验证成功");
        }else {
            Log.e("LoginAPI", "验证错误");
        }
        return login_ret;
    }

    private static void saveLogin(String username,String nickname,int corp_id,String phone,String email){
        Log.e("LoginAPI", "保存登陆数据");
        SharedPreferences preferences = LoginActivity.loginactivity.getSharedPreferences(USER_INFO_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        // 存入数据
        editor.putString("username", username);
        editor.putString("nickname", nickname);
        editor.putInt("corp_id", corp_id);
        editor.putString("phone", phone);
        editor.putString("email", email);
        // 提交修改
        editor.commit();
    }
}
